--    Program: 		CAPARIMP_INVOICE_LINE_SALE.sql
--    Description:  Import AR INVOICE LINE SALE information 
--    Revision:     1st     
--    Parameter: 	
--    Created By:   Kenny Leung
--    Date:         2-Feb-2006

set doc off;      
set verify off;
set serveroutput on size 1000000;
--whenever sqlerror exit failure rollback;

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
   l_err_msg_tmp			varchar2(2000);
   
   l_cust_trx_line_salesrep_id	number;
                    		                 		
   cursor cur_ar_invoice_line_sale is
      select * from CRST_RA_CUST_TRX_LINE_SALESREP
      for update;

begin
   l_processed_count := 0;
   l_rejected_count  := 0;
   l_err_msg := '';
   
   for cur in cur_ar_invoice_line_sale loop   	
     l_err_msg_tmp := '';
     
     SELECT RA_CUST_TRX_LINE_SALESREPS_S.NEXTVAL 
     INTO l_cust_trx_line_salesrep_id
     FROM DUAL;
     
     update crst_ra_cust_trx_line_salesrep
     set    cust_trx_line_salesrep_id_n = l_cust_trx_line_salesrep_id
     where  cust_trx_line_salesrep_id = cur.CUST_TRX_LINE_SALESREP_ID;
     
     begin
       -- import data
       insert into RA_CUST_TRX_LINE_SALESREPS_ALL
       (
          CUST_TRX_LINE_SALESREP_ID       ,
          LAST_UPDATE_DATE                ,
          LAST_UPDATED_BY                 ,
          CREATION_DATE                   ,
          CREATED_BY                      ,
          LAST_UPDATE_LOGIN               ,
          CUSTOMER_TRX_ID                 ,
          SALESREP_ID                     ,
          CUSTOMER_TRX_LINE_ID            ,
          REVENUE_AMOUNT_SPLIT            ,
          NON_REVENUE_AMOUNT_SPLIT        ,
          ATTRIBUTE_CATEGORY              ,
          ATTRIBUTE1                      ,
          ATTRIBUTE2                      ,
          ATTRIBUTE3                      ,
          ATTRIBUTE4                      ,
          ATTRIBUTE5                      ,
          ATTRIBUTE6                      ,
          ATTRIBUTE7                      ,
          ATTRIBUTE8                      ,
          ATTRIBUTE9                      ,
          ATTRIBUTE10                     ,
          REQUEST_ID                      ,
          PROGRAM_APPLICATION_ID          ,
          PROGRAM_ID                      ,
          PROGRAM_UPDATE_DATE             ,
          NON_REVENUE_PERCENT_SPLIT       ,
          REVENUE_PERCENT_SPLIT           ,
          ORIGINAL_LINE_SALESREP_ID       ,
          PREV_CUST_TRX_LINE_SALESREP_ID  ,
          ATTRIBUTE11                     ,
          ATTRIBUTE12                     ,
          ATTRIBUTE13                     ,
          ATTRIBUTE14                     ,
          ATTRIBUTE15                     ,
          ORG_ID                          ,
          WH_UPDATE_DATE                  ,
          REVENUE_ADJUSTMENT_ID                      
       )
       values
       (
            l_cust_trx_line_salesrep_id         ,
            cur.LAST_UPDATE_DATE                ,
            CAPBRIMP_MAPPING.GetUserMapping(cur.LAST_UPDATED_BY),
            cur.CREATION_DATE                   ,
            CAPBRIMP_MAPPING.GetUserMapping(cur.CREATED_BY),
            cur.LAST_UPDATE_LOGIN               ,
            CAPARIMP_MAPPING.GetCustomerTrxIDMapping(cur.CUSTOMER_TRX_ID),
            CAPARIMP_MAPPING.GetSalesRepIDMapping(cur.ORG_ID, cur.SALESREP_ID),
            CAPARIMP_MAPPING.GetCustomerTrxLineIDMapping(cur.CUSTOMER_TRX_LINE_ID),
            cur.REVENUE_AMOUNT_SPLIT            ,
            cur.NON_REVENUE_AMOUNT_SPLIT        ,
            cur.ATTRIBUTE_CATEGORY              ,
            cur.ATTRIBUTE1                      ,
            cur.ATTRIBUTE2                      ,
            cur.ATTRIBUTE3                      ,
            cur.ATTRIBUTE4                      ,
            cur.ATTRIBUTE5                      ,
            cur.ATTRIBUTE6                      ,
            cur.ATTRIBUTE7                      ,
            cur.ATTRIBUTE8                      ,
            cur.ATTRIBUTE9                      ,
            cur.ATTRIBUTE10                     ,
            cur.REQUEST_ID                      ,
            cur.PROGRAM_APPLICATION_ID          ,
            cur.PROGRAM_ID                      ,
            cur.PROGRAM_UPDATE_DATE             ,
            cur.NON_REVENUE_PERCENT_SPLIT       ,
            cur.REVENUE_PERCENT_SPLIT           ,
            cur.ORIGINAL_LINE_SALESREP_ID       ,
            cur.PREV_CUST_TRX_LINE_SALESREP_ID  ,
            cur.ATTRIBUTE11                     ,
            cur.ATTRIBUTE12                     ,
            cur.ATTRIBUTE13                     ,
            cur.ATTRIBUTE14                     ,
            cur.ATTRIBUTE15                     ,
            CAPBRIMP_MAPPING.GetOrgMapping(cur.ORG_ID),
            cur.WH_UPDATE_DATE                  ,
            cur.REVENUE_ADJUSTMENT_ID                                  
       );  
     exception
       when others then
     	 l_err_msg_tmp := sqlerrm;
     end;	  	    
     
     if l_err_msg_tmp = '' then
       l_processed_count := l_processed_count + 1;  
     else
       l_rejected_count := l_rejected_count + 1;
       l_err_msg := l_err_msg_tmp; 
     end if;
   end loop;

   if l_rejected_count <> 0 0 then
   	 commit;
   else
     rollback;
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPARIMP_INVOICE_LINE_SALE', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
